<?php
session_start();
?>
<html>
<?php
    
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
    // last request was more than 30 minates ago
    session_unset();     // unset $_SESSION variable for the runtime 
    session_destroy();   // destroy session data in storage
    
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'index.php';
    header("Location: http://$host$uri/$extra?expire=true");
    exit;
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
?>     
    <head>
        
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Botany Dichotomous Key Conversion System</title>
        <!--[if IE]>
            <link rel="stylesheet" type="text/css" href="css/defaultIE.css" />
            <link rel="stylesheet" type="text/css" href="css/stepsMenu.css" />
        <![endif]-->
        <!--[if !IE]><!-->
            <link rel="stylesheet" type="text/css" href="css/default.css" />
            <link rel="stylesheet" type="text/css" href="css/stepsMenu.css" />
         <!--<![endif]-->
        
	<script language="javascript">
                var items=0;
                function AddGlossaryItem() {
                    div=document.getElementById("items");
                    button=document.getElementById("add");
                    items++;
                    glossary="<br /><h4>Glossary Term " + items + " </h4><br />";
                    title="<label class='glossaryTitle'><b> Title : </b></label>";
                    title+="<input type=\"text\" name=\"glossaryTitle" + items;
                    title+="\" size=\"45\"><br><br>";
                    description="<label class='glossaryTitle'><b> Description :</b></label>";
                    description+="<input type=\"text\" name=\"glossaryDescription" + items;
                    description+="\" size=\"45\"><br><br>";
                    image="<label class='glossaryTitle'><b> Image :</b></label>";
                    image+="<input type=\"file\" name=\"glossaryImage" + items;
                    image+="\" id=\"glossaryImage" + items;
                    image+="\" size=\"45\" onChange=\"load_glossaryImage(this.id,this.value)\"><br /><br />";
                    //addImageButton="<input type=\"button\" name=\"glossaryAddImageButton" + items;
                    //addImageButton+="\" value=\"Add Another Image\"><br><br>";
                    
                    newnode1=document.createElement("span");
                    newnode1.innerHTML=glossary;
                    newnode2=document.createElement("span");
                    newnode2.innerHTML=title;
                    newnode3=document.createElement("span");
                    newnode3.innerHTML=description;
                    newnode4=document.createElement("span");
                    newnode4.innerHTML=image;
                    //newnode5=document.createElement("span");
                    //newnode5.innerHTML=addImageButton;
                    div.insertBefore(newnode1,button);
                    div.insertBefore(newnode2,button);
                    div.insertBefore(newnode3,button);
                    div.insertBefore(newnode4,button);
                    //div.insertBefore(newnode5,button);
                    
                    document.getElementById('glossaryItemsCount').value = items;      
                }        
        </script>
        <script src="javascripts/imageCheck.js"></script>
    </head>
	
    <body>
 
       <?php 
            $uploaddir= $_POST["output"];
            $inputFile = $uploaddir."keyconversion.xml";
            //echo "The file is ". $inputFile;
            $xml = simplexml_load_file($inputFile);
            $sxe = new SimpleXMLElement($xml->asXML());

            $Orgcount = count($xml->organisms->organism);
            for($i = 0; $i < $Orgcount; $i++){
                $orgIDno = $xml->organisms->organism[$i]->org_id;
                $orgID = "Org".$xml->organisms->organism[$i]->org_id;
                $organismNode = $sxe->organisms[0]->organism[$i];

                /*************************/
                $orgImageCount = count($organismNode->image);
                $temp = ($organismNode->count())-$orgImageCount;
                if($temp > 2){
                    unset($organismNode->family);
                    unset($organismNode->genus);
                    unset($organismNode->species);
                    unset($organismNode->leaves);
                    unset($organismNode->inflorescence);
                    unset($organismNode->flower);
                    unset($organismNode->fruit);
                    unset($organismNode->ecology);
                    unset($organismNode->seeds);
                    unset($organismNode->elevation);
                    unset($organismNode->bioregional_distribution);
                    unset($organismNode->taxon_id);
                    unset($organismNode->jepson_url);
                }
                /*************************/
                
                $orgFamilyName = $orgID."Family";
                $organismNode->addChild("family", $_POST["$orgFamilyName"]);

                $orgGenusName = $orgID."Genus";
                $organismNode->addChild("genus", $_POST["$orgGenusName"]);

                $orgSpeciesName = $orgID."Species";
                $organismNode->addChild("species", $_POST["$orgSpeciesName"]);

                $orgLeavesName = $orgID."Leaves";
                $organismNode->addChild("leaves", $_POST["$orgLeavesName"]);
                //echo $_POST["$orgLeavesName"]."<br />";

                $orgInflorescenceName = $orgID."Inflorescence";
                $organismNode->addChild("inflorescence", $_POST["$orgInflorescenceName"]);
                //echo $_POST["$orgInflorescenceName"]."<br />"; 

                $orgFlowerName = $orgID."Flower";
                $organismNode->addChild("flower", $_POST["$orgFlowerName"]);
                //echo $_POST["$orgFlowerName"]."<br />"; 

                $orgFruitName = $orgID."Fruit";
                $organismNode->addChild("fruit", $_POST["$orgFruitName"]);
                //echo $_POST["$orgFruitName"]."<br />"; 

                $orgEcologyName = $orgID."Ecology";
                $organismNode->addChild("ecology", $_POST["$orgEcologyName"]);
                //echo $_POST["$orgEcologyName"]."<br />";

                $orgSeedsName = $orgID."Seeds";
                $organismNode->addChild("seeds", $_POST["$orgSeedsName"]);
                //echo $_POST["$orgSeedsName"]."<br />"; 

                $orgElevationName = $orgID."Elevation";
                $organismNode->addChild("elevation", $_POST["$orgElevationName"]);
                //echo $_POST["$orgElevationName"]."<br />";

                $orgBioregional_DistributionName = $orgID."Bioregional_Distribution";
                $organismNode->addChild("bioregional_distribution", $_POST["$orgBioregional_DistributionName"]);
                //echo $_POST["$orgBioregional_DistributionName"]."<br />"; 

                $orgTaxon_idName = $orgID."Taxon_id";
                $organismNode->addChild("taxon_id", $_POST["$orgTaxon_idName"]);
                //echo $_POST["$orgTaxon_idName"]."<br />"; 

                $orgJepson_urlName = $orgID."Jepson_url";
                $organismNode->addChild("jepson_url", $_POST["$orgJepson_urlName"]);
                //echo $_POST["$orgJepson_urlName"]."<br />"; 

                // FOR PROPER FORMATTING XML
                // Create a new DOMDocument object
                $dom = new DOMDocument('1.0');
                // add spaces, new lines and make the XML more readable format        
                $dom->preserveWhiteSpace = false;
                $dom->formatOutput = true;
                //Load XML document from a string
                $dom->loadXML($sxe->asXML());
                //Save XML to file 
                $dom->save($inputFile);
            }
        ?>
        

        <?php include("includes/header.php"); ?>
	 <div id="page-wrap2">
                <div id="main-content">

                    <div id="left-col">
                        <?php include("includes/menuAddglossary.php"); ?>
                    </div><!-- end of left-col -->
                    
                    <div id = "right-col">
                        <h3>Step 5: Enter glossary information</h3>
                        <form action="Confirm.php" method="post" enctype="multipart/form-data">
                            <fieldset id="glossaryField">
                                <p class="instruction">Please enter glossary terms for the dichotomous key using <i>"Add Glossary Term"</i>. 
                                You can upload multiple glossary terms. Uploading glossary terms is optional.</p>
                                <div ID="items">
                                    <!-- <h4 id ="glossary">Glossary Term 1:</h4> <br />
                                    <b>Title :</b>
                                    <input type="text" name="glossaryTitle1" size="45"><br /><br />
                                    <b>Description :</b>
                                    <input type="text" name="glossaryDescription1" size="45"><br /><br />
                                    <b>Image :</b>
                                    <input type="file" name="glossaryImage1" size="45"> -->
                                    <input type="hidden" id="glossaryItemsCount" name="glossaryItemsCount"/>
                                    <input type="button" value="Add Glossary Item" onClick="AddGlossaryItem();" id="add">
                                </div>
                                
                          <div style="text-align:center;">
                           <input type="hidden" name="output" value=<?php echo $uploaddir ?> /> <br/><br/>
                           <p class="instruction">(When you click <i>Submit</i> button, you will
                               be asked to download .ZIP file.)</p>
                            <input type="submit" class="next" value="Submit"/> <br /><br />
                          </div>
                            
                            </fieldset>
                    
                        </form>
                        </div><!-- end of right-col -->

                    <div class="clear"></div> <!-- all the content inside main-content div element is floated, need to clear float...
                                              because height of main-content does not know how tall it would be -->

                </div><!-- end of main-content -->        
            </div>  <!-- End of page-wrap -->    
        <?php include("includes/footer.php"); ?>
                
    </body>
</html> 